package com.ceba.test.modules.admin.dao;

import abc.ID;
import abc.data.*;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.object.XObject;
import com.ceba.base.web.dao.IBaseDao;
import com.ceba.test.modules.admin.entity.GWaterPageNumber;

import java.text.SimpleDateFormat;
import java.util.Date;

public class WaterPageNumberDao extends IBaseDao{

    /**
     * c创建一条记录
     * @param gWaterPageNumber
     * @return
     */
    public boolean addWaterPageNumber(GWaterPageNumber gWaterPageNumber) throws Exception{
        IStore store = StoreManager.create("g_water_page_number");
        IObject object = new XObject("g_water_page_number");
        object.put("ID", ID.getGuid());
        object.put("G_STAMP_ID",gWaterPageNumber.getgStampId());
        object.put("SELECT_FLAG",gWaterPageNumber.getSelectFlag());
        object.put("SELECT_PAGE_NUMBER",gWaterPageNumber.getSelectPageNumber());
        object.put("RADIO_FLAG",gWaterPageNumber.getRadioFlag());
        object.put("CREATE_USER",gWaterPageNumber.getCreateUser());
        object.setCreateTime();
        if(store.insert(object)>0){
            return true;
        }
        return false;
    }

    /**
     * 修改记录
     * @param gWaterPageNumber
     * @return
     */
    public boolean updateWaterPageNumber(GWaterPageNumber gWaterPageNumber) throws Exception{
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        Sql sql = new Sql("UPDATE g_water_page_number SET SELECT_FLAG='"+gWaterPageNumber.getSelectFlag()+"'," +
                "RADIO_FLAG='"+gWaterPageNumber.getRadioFlag()+"'," +
                "SELECT_PAGE_NUMBER='"+gWaterPageNumber.getSelectPageNumber()+"'," +
                "UPDATE_USER='"+gWaterPageNumber.getUpdateUser()+"'," +
                "UPDATE_TIME='"+dateString+"' where G_STAMP_ID='"+gWaterPageNumber.getgStampId()+"'");

       boolean flag = SqlSessionManager.execute(sql);
        return flag;
    }

    /**
     * 根据印章ID查询记录
     * @param stampId
     * @return
     */
    public GWaterPageNumber queryWaterPageNumberByStampId(String stampId) throws Exception{
        Sql sql = new Sql("select * from g_water_page_number where G_STAMP_ID='"+stampId+"'");
        GWaterPageNumber gWaterPageNumber = new GWaterPageNumber();
        DataTable dataTable = SqlSessionManager.getDataTable(sql);
        if(dataTable!=null&&dataTable.getRowCount()>0){
            DataRow dataRow = dataTable.getRow(0);
            gWaterPageNumber.setId("ID");
            gWaterPageNumber.setgStampId(dataRow.getString("G_STAMP_ID"));
            gWaterPageNumber.setSelectFlag(dataRow.getString("SELECT_FLAG"));
            gWaterPageNumber.setSelectPageNumber(dataRow.getString("SELECT_PAGE_NUMBER"));
            gWaterPageNumber.setRadioFlag(dataRow.getString("RADIO_FLAG"));
            gWaterPageNumber.setCreateUser(dataRow.getString("CREATE_USER"));
            gWaterPageNumber.setCreateTime(dataRow.getString("CREATE_TIME"));
            gWaterPageNumber.setUpdateTime(dataRow.getString("UPDATE_TIME"));
            gWaterPageNumber.setUpdateUser(dataRow.getString("UPDATE_USER"));
        }
        return gWaterPageNumber;
    }

    /**
     * 根据签章ID删除一条记录
     * @param stampId
     * @return
     * @throws Exception
     */
    public boolean deleteWaterPageNumberByStampId(String stampId) throws Exception{
        Sql sql = new Sql("delete from g_water_page_number where G_STAMP_ID='"+stampId+"'");
        boolean flag = SqlSessionManager.execute(sql);
        return flag;
    }
}
